//
//  AZPickerView.m
//  hnGovernment
//
//  Created by 安仔 on 17/3/9.
//  Copyright © 2017年 安仔. All rights reserved.
//

#import "AZPickerView.h"

@interface AZPickerView ()<UIPickerViewDelegate, UIPickerViewDataSource>
@property (nonatomic, copy) NSArray *dataArr;
@property (nonatomic, strong) UIView *bottomView;
@property (nonatomic, strong) UIButton *leftBtn;
@property (nonatomic, strong) UIButton *rightBtn;
@property (nonatomic, strong) UIPickerView *myPickerView;
@property (nonatomic, strong) NSString *resultStr;


@end

@implementation AZPickerView

- (instancetype) initWithFrame:(CGRect)frame withContentArr:(NSArray *)arr {
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor colorWithWhite:0.0 alpha:0.35];
        self.dataArr = arr;
        [self initPickerView];
    }
    return self;
}

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
    return 1;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
    return self.dataArr.count;
}

- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component {
    return 88 * m6Scale;
}

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
    
    return self.dataArr[row];
}

//重写方法
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view {
    UILabel* pickerLabel = (UILabel *)view;
    if (!pickerLabel){
        pickerLabel = [[UILabel alloc] init];
        pickerLabel.adjustsFontSizeToFitWidth = YES;
        [pickerLabel setTextAlignment:NSTextAlignmentCenter];
        [pickerLabel setBackgroundColor:[UIColor clearColor]];
        [pickerLabel setFont:[UIFont systemFontOfSize:28 * m6Scale]];
        [pickerLabel setTextColor:[UIColor colorWithHexString:@"333333" alpha:1.0]];
    }
    pickerLabel.text=[self pickerView:pickerView titleForRow:row forComponent:component];
    return pickerLabel;
}

#pragma mark  pickerView
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
    
    self.resultStr = self.dataArr[row];
}

#pragma mark  Button的点击方法
- (void)clickLeftBtn {

    if ([self.delegate respondsToSelector:@selector(AZPickerViewLeftBtn)]) {
        [self.delegate AZPickerViewLeftBtn];
    }
}

- (void)clickRightBtn {
    if ([self.delegate respondsToSelector:@selector(AZPickerViewRightBtnWithResult:)]) {
        [self.delegate AZPickerViewRightBtnWithResult:self.resultStr];
    }
}

- (void)initPickerView {
    [self addSubview:self.bottomView];
    [self addSubview:self.myPickerView];
    [self.bottomView addSubview:self.leftBtn];
    [self.bottomView addSubview:self.rightBtn];
}

- (void) layoutSubviews {
    
    [super layoutSubviews];
    [self.bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.mas_centerX);
        make.bottom.mas_equalTo(self.mas_bottom);
        make.size.mas_equalTo(CGSizeMake(KScreenWidth, 385 * m6Scale));
    }];
    
    [self.myPickerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.mas_centerX);
        make.bottom.mas_equalTo(self.mas_bottom);
        make.size.mas_equalTo(CGSizeMake(KScreenWidth, 285 * m6Scale));
    }];
    
    [self.leftBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.bottomView.mas_left).offset(20 * m6Scale);
        make.top.mas_equalTo(self.bottomView.mas_top).offset(26 * m6Scale);
        make.size.mas_equalTo(CGSizeMake(80 * m6Scale, 50 * m6Scale));
    }];
    
    [self.rightBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(self.bottomView.mas_right).offset(-20 * m6Scale);
        make.top.mas_equalTo(self.bottomView.mas_top).offset(26 * m6Scale);
        make.size.mas_equalTo(CGSizeMake(80 * m6Scale, 50 * m6Scale));
    }];
}

- (UIButton *)rightBtn {
    
    if (!_rightBtn) {
        _rightBtn = [[UIButton alloc] init];
        [_rightBtn setTitle:NSLocalString(@"完成") forState:0];
        [_rightBtn setTitleColor:APPColor forState:0];
        _rightBtn.titleLabel.font = [UIFont systemFontOfSize:32 * m6Scale];
        [_rightBtn addTarget:self action:@selector(clickRightBtn) forControlEvents:UIControlEventTouchUpInside];
    }
    return _rightBtn;
}

- (UIButton *)leftBtn {
    
    if (!_leftBtn) {
        _leftBtn = [[UIButton alloc] init];
        [_leftBtn setTitle:NSLocalString(@"返回") forState:0];
        [_leftBtn setTitleColor:APPColor forState:0];
        _leftBtn.titleLabel.font = [UIFont systemFontOfSize:32 * m6Scale];
        [_leftBtn addTarget:self action:@selector(clickLeftBtn) forControlEvents:UIControlEventTouchUpInside];
    }
    return _leftBtn;
}

- (UIPickerView *)myPickerView {
    if (!_myPickerView) {
        _myPickerView = [[UIPickerView alloc] init];
        _myPickerView.backgroundColor = BackgroundColor;
        _myPickerView.delegate = self;
        _myPickerView.dataSource = self;
    }
    return _myPickerView;
}

- (UIView *)bottomView {
    
    if (!_bottomView) {
        _bottomView = [[UIView alloc] init];
        _bottomView.backgroundColor = [UIColor whiteColor];
    }
    return _bottomView;
}

@end
